package com.kara.webapp.services.impl.encoder;

import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.kara.entity.Clazz;
import com.kara.service.ClazzManager;

public class ClazzEncoder implements ValueEncoder<Clazz> {

	ClazzManager clazzManager;
	
	public ClazzEncoder(ClazzManager clazzManager) {
		this.clazzManager = clazzManager;
	}

	@Override
	public String toClient(Clazz clazz) {
		if (clazz!= null){
			return String.valueOf(clazz.getId());
		}
		return null;
	}

	@Override
	public Clazz toValue(String clientValue) {
		if (clientValue ==null || "null".equals(clientValue)){
			return null;
		}
		long clazzId;
		try {
			clazzId = Long.parseLong(clientValue);
		}catch (Exception ne){
			return new Clazz();
		}
		return clazzManager.getClazz(clazzId);
	
	}

}
